/*
 * 创建日期 2010-4-16
 *
 * 成都天和软件公司
 * 电话：028-85425861
 * 传真：028-85425861-8008
 * 邮编：610041 
 * 版权所有
 */
package  org.fantasy.cpp.core.html.edit;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import  org.fantasy.cpp.core.bean.ParamContext;
import  org.fantasy.common.db.bean.Row;
import  org.fantasy.common.util.StringUtil;

/**
 *  普通下拉列表框
 * @author: 王文成
 * @version: 1.0
 * @since 2010-4-16
 */
public class SelectHtml extends AbstractEditHtml {

	public SelectHtml(HttpServletRequest request, ParamContext param) {
		super(request, param);
	}

	@SuppressWarnings("all")
	public String getHtml() throws Exception {
		List<Row> options = (List<Row>) getSource();
		StringBuffer html = new StringBuffer(1024);
		html.append("<select " + getStyleHtml() + getValidate() + " id=\"" + name + "\" name=\"" + name + "\"");
		html.append(getClassHtml() + getMultiple() + "> \n");
		html.append("      " + getOptionsHtml(options, getValue()) + " \n");
		html.append("</select> \n");
		return html.toString();
	}

	/**
	 * 生成Option
	 * 
	 * @return
	 */
	protected String getOptionsHtml(List<Row> options, String setValue) {
		StringBuffer html = new StringBuffer(1024);
		html.append("<option value=''>[请选择]</option> \n");
		for (Row bean : options) {
			String value = StringUtil.tryThese(bean.getString("id"),bean.getString("value"));
			String text = StringUtil.tryThese(bean.getString("name"),bean.getString("text"));
			if (value.equals(setValue))
				html.append("<option value=\"" + value + "\" selected=\"true\">" + text + "</option> \n");
			else
				html.append("<option value=\"" + value + "\">" + text + "</option> \n");
		}
		return html.toString();
	}
}
